package com.github.linmt.dbdoc4j.utils;

import java.util.Map;
import java.util.Properties;

/**
 * User: Linmt
 * Date: 2022/12/2
 * Time: 20:20
 * Description: No Description
 */
public class DbUrlUtils {

    public static Properties parseUrlParam(String url) {
        Properties urlParam = new Properties();
        int index = url.indexOf("?");
        if (index != -1) {
            String coreUrl = url.substring(index);

            String[] params = coreUrl.split("&");
            for (String param : params) {
                String[] kv = param.split("=");

                if (kv.length == 1) {
                    urlParam.put(kv[0], null);
                } else if (kv.length == 2) {
                    urlParam.put(kv[0], kv[1]);
                }
            }
        }

        return urlParam;
    }

    public static String appendUrlParam(String originUrl, Properties params) {
        int index = originUrl.indexOf("?");
        for (Map.Entry<Object, Object> param : params.entrySet()) {
            String paramStr = param.getKey() + "=" + param.getValue();
            originUrl += (index++ == -1 ? "?" : "&") + paramStr;
        }

        return originUrl;
    }
}
